package com.HospitalOutpatient.dao;

import com.HospitalOutpatient.dto.DoctorDutyDto;
import com.HospitalOutpatient.entity.Doctor;
import com.HospitalOutpatient.entity.Duty;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface DutyDao {

    int insertDuty(List<Duty> dutyList);

    @Delete("delete from duty where dutyDate = #{dutyDate} and doctorId = #{doctorId}")
    void deleteDutyByDate(Duty duty);

    @Select("select * from duty where doctorId = #{doctorId} and dutyDate = #{dutyDate}")
    List<Duty> selectAllDutyByIdAndDate(Duty duty);

    @Select("select * from duty where outpatientId = #{outpatientId} and dutyDate = #{dutyDate} and now() < endTime group by doctorId")
    List<Duty> selectDutyByOutpatientIdAndDatethanTime(DoctorDutyDto doctorDutyDto);

    @Select("select sum(partialNum) from duty where doctorId = #{doctorId} and dutyDate = #{dutyDate} group by doctorId")
    int selectSumpartialNumByDoctorId(Duty duty);

    @Select("select sum(totalNum) from duty where doctorId = #{doctorId} and dutyDate = #{dutyDate} group by doctorId")
    int selectSumtotalNumByDoctorId(Duty duty);

    @Select("select * from duty where doctorId = #{doctorId} and dutyDate = #{dutyDate} and now() < endTime and partialNum > 0")
    List<Duty> selectDutyByDoctorIdAndDateTime(Duty duty);

    @Update("update duty set partialNum = partialNum-1 where dutyId = #{dutyId}")
    int updateDutyById(String dutyId);

    @Select("select * from duty where dutyId = #{dutyId}")
    Duty selectDutyById(String dutyId);

    @Select("select * from duty where doctorId = #{doctorId} and now() < endTime and dutyDate = curdate()")
    List<Duty> selectDutyByDoctorIdAndDate(String doctorId);

    @Select("select * from duty where doctorId = #{doctorId}")
    List<Duty> selectFinishDutyByDoctorId(String doctorId);

    @Update("update duty set partialNum = partialNum+1 where dutyId = #{dutyId}")
    int updateAddDutyById(String dutyId);


}
